import storage from '@/utils/storage'
import { ACCESS_TOKEN, CURRENT_USER } from '@/store/constantKeys'

const state = {
  token: storage.get(ACCESS_TOKEN, ''),
  userInfo: storage.get(CURRENT_USER, {})
}

const mutations = {
  setUser: (state, info) => {
    state.userInfo = info
  },
  setToken: (state, token) => {
    state.token = token
  },
  clearUser: (state) => {
    state.userInfo = {}
    state.token = ''
  }
}

const actions = {
  loginOut: ({ commit }) => {
    storage.clear()
    storage.clearCookie()
    commit('clearUser')
  }
}

const getters = {
  token: (state) => state.token,
  userInfo: (state) => state.userInfo
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
